#include "browser.h"

void
parse_argu (msg * irc, char *buffer)
{
  char *runner = buffer;
  unsigned int n;
  irc->cc = 0;
  irc->argu = (char **) malloc (sizeof (char *));

  while (*buffer != 0)
    {
      for (; *buffer != ' ' && *buffer; buffer++);
      n = buffer - runner;
      irc->argu =
	(char **) realloc (irc->argu, (irc->cc + 1) * sizeof (char *));
      *(irc->argu + irc->cc) = (char *) malloc (n + 1);

      strncpy (*(irc->argu + irc->cc), runner, n);
      *(irc->argu[irc->cc] + n) = 0;
      irc->cc++;

      while (*buffer == ' ')
	buffer++;
      runner = buffer;
    }
}

void
clean_txt (char *txt)
{
  int a;
  if (!txt)
    return;
  for (a = 0; txt[a] != 0; a++)
    if ((txt[a] == '\n') || (txt[a] == '\r'))
      {
	txt[a] = 0;
	return;
      }
}

void
free_msg (msg * i)
{
  int a;
  for (a = 0; a < i->cc; a++)
    free (i->argu[a]);
  free (i->argu);
}

char *
argu_to_string (char **argu, int nbarg, char *radical, int depart)
{
  int run, taille = 0;
  char *buck;

  for (run = depart; run < nbarg; run++)
    taille += strlen (argu[run]);
  buck = (char *) malloc (taille + strlen (radical) + nbarg + 2);

  strcpy (buck, radical);
  for (run = depart; run < nbarg; run++)
    {
      strcat (buck, argu[run]);
      if (run + 1 < nbarg)
	strcat (buck, " ");
    }
  return buck;
}
